package tools;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;

import application.CanvasView;

public class EraserTool extends Tool
{
	// Components
	protected CanvasView canvas;
	protected Point startingMousePosition;
	protected Color saveColor;
	
	// Constructor
	public EraserTool(CanvasView c)
	{
		if( c != null )
			canvas = c;
	    else
	    	throw new IllegalArgumentException();
	}
	
	// Eraser draw method
	protected void drawErasure(int x, int y, int width, int height)
	{
		Graphics iBGraphics = canvas.getimageBufferGraphics();
	    iBGraphics.fillRect(x, y, width, height);
	    canvas.repaint(x, y, width, height);
	}
	
	/*******Event Methods***********************/
	/* 
	 * Establishes the starting point for erasing and sets the "drawing" color
	 * to the background color. 
	 * 
	 * @see tools.Tool#mousePressed(java.awt.event.MouseEvent)
	 */
	public void mousePressed(MouseEvent e)  
	{
		startingMousePosition = e.getPoint();
	    Graphics iBGraphics = canvas.getimageBufferGraphics();
	    saveColor = iBGraphics.getColor( );
	    iBGraphics.setColor( canvas.BACKGROUND );
	}

	/*  
	 * Erases as the user drags the mouse across the canvas.
	 * 
	 * @see tools.Tool#mouseDragged(java.awt.event.MouseEvent)
	 */
	public void mouseDragged(MouseEvent e)  {
		/* erase  */
	    Point newMousePosition = e.getPoint();
	    int x0 = Math.min(startingMousePosition.x, newMousePosition.x)-2;
	    int y0= Math.min(startingMousePosition.y, newMousePosition.y)-2;
	    int dx = Math.abs(newMousePosition.x - startingMousePosition.x) + 5;
	    int dy = Math.abs(newMousePosition.y - startingMousePosition.y) + 5;
	    drawErasure(x0, y0, dx, dy);
	    
	    /* update current mouse coordinates */
	    startingMousePosition = newMousePosition;
	}

	/* 
	 * Completes the eraser and resets the drawing color to the previous pen
	 * color.
	 * 
	 * @see tools.Tool#mouseReleased(java.awt.event.MouseEvent)
	 */
	public void mouseReleased(MouseEvent e) { 
		Graphics iBGraphics = canvas.getimageBufferGraphics();
	    
		/* restore pen color  */
	    iBGraphics.setColor(saveColor);
	}
}
